#include "etcder.hpp"
#include "channel.hpp"
#include "base.pb.h"
#include "message.pb.h"
#include "user.pb.h"
#include "file.pb.h"
#include "uuid.hpp"
#include <gflags/gflags.h>
#include <thread>
#include <gtest/gtest.h>
#include <iostream>
#include <string>
#include <vector>
#include <unordered_set>

DEFINE_bool(log_mode, false, "程序的运行模式: true-发布模式 false-调试模式");
DEFINE_string(log_file, "", "发布模式下，制定日志的输出文件");
DEFINE_int32(log_level, 0, "发布模式下，制定日志的输出等级");

DEFINE_string(etcd_host, "127.0.0.1:2379", "服务注册中心地址");
DEFINE_string(base_service, "/service", "服务监测根目录");

DEFINE_string(service, "/service/message_service", "服务名称");

bite_im::ChannelPtr channel;

/*TEST(消息子服务测试, 根据时间搜索会话的消息1)
{
    std::cout<<"测试2024-10-07 16:08:40 到 2024-10-08 08:12:58的消息"<<std::endl;
    bite_im::MsgStorageService_Stub stub(channel.get());
    // 进行rpc调用

    bite_im::GetHistoryMsgReq req;

    req.set_request_id(bite_im::UUID::uuid());
    req.set_chat_session_id("会话-1");
    // date -d "2024-10-07 16:08:40" +%s 通过linux命令
    req.set_start_time(1728288520);
    req.set_over_time(1728346258);
    // req.set_over_time(boost::posix_time::to_time_t(2024-10-08 08:13:58));

    bite_im::GetHistoryMsgRsp resp;
    // 获取一个rpc操作对象
    brpc::Controller cntl;
    // stub调用我们实现的服务类ipml的接口
    stub.GetHistoryMsg(&cntl, &req, &resp, nullptr);

    // ASSERT_TRUE(resp.success());

    // 检查一下会话1中是否符合
    auto message_info_list = resp.msg_list();

    for (const auto &message_info : message_info_list)
    {
        std::cout << message_info.message_id() << std::endl;
        std::cout << message_info.chat_session_id() << std::endl;
        std::cout << message_info.timestamp() << std::endl;
        // std::cout << message_info.sender().nickname() << std::endl;  // 我们在测试时的nickname还没有设置这里就是空
        std::cout << message_info.message().message_type() << std::endl;
        switch (message_info.message().message_type())
        {
        case bite_im::MessageType::STRING:
        {
            std::cout << message_info.message().string_message().content() << std::endl;
        }
        break;
        case bite_im::MessageType::IMAGE:
        {
            std::cout << message_info.message().image_message().image_content() << std::endl;
        }
        break;
        case bite_im::MessageType::FILE:
        {
            std::cout << message_info.message().file_message().file_contents() << std::endl;
        }
        break;
        case bite_im::MessageType::SPEECH:
        {
            std::cout << message_info.message().speech_message().file_contents() << std::endl;
        }
        break;
        default:
            LOG_ERROR("文件类型[{}]错误", message_info.message().message_type());
        break;
        }
    }
}
*/
/*TEST(消息子服务测试, 根据时间搜索会话的消息2)
{
    std::cout<<"测试2024-10-08 08:12:58 到 2024-10-08 08:13:58的消息"<<std::endl;
    bite_im::MsgStorageService_Stub stub(channel.get());
    // 进行rpc调用

    bite_im::GetHistoryMsgReq req;

    req.set_request_id(bite_im::UUID::uuid());
    req.set_chat_session_id("会话-1");
    // date -d "2024-10-08 08:13:58" +%s 通过linux命令
    req.set_start_time(1728346438);
    req.set_over_time(1728346439);
    // req.set_over_time(boost::posix_time::to_time_t(2024-10-08 08:13:58));

    bite_im::GetHistoryMsgRsp resp;
    // 获取一个rpc操作对象
    brpc::Controller cntl;
    // stub调用我们实现的服务类ipml的接口
    stub.GetHistoryMsg(&cntl, &req, &resp, nullptr);

    // ASSERT_TRUE(resp.success());

    // 检查一下会话1中是否符合
    auto message_info_list = resp.msg_list();

    for (const auto &message_info : message_info_list)
    {
        std::cout << message_info.message_id() << std::endl;
        std::cout << message_info.chat_session_id() << std::endl;
        std::cout << message_info.timestamp() << std::endl;
        // std::cout << message_info.sender().nickname() << std::endl;  // 我们在测试时的nickname还没有设置这里就是空
        std::cout << message_info.message().message_type() << std::endl;
        switch (message_info.message().message_type())
        {
        case bite_im::MessageType::STRING:
        {
            std::cout << message_info.message().string_message().content() << std::endl;
        }
        break;
        case bite_im::MessageType::IMAGE:
        {
            std::cout << message_info.message().image_message().image_content() << std::endl;
        }
        break;
        case bite_im::MessageType::FILE:
        {
            std::cout << message_info.message().file_message().file_contents() << std::endl;
        }
        break;
        case bite_im::MessageType::SPEECH:
        {
            std::cout << message_info.message().speech_message().file_contents() << std::endl;
        }
        break;
        default:
            LOG_ERROR("文件类型[{}]错误", message_info.message().message_type());
        break;
        }
    }
}
*/
TEST(消息子服务测试, 某个会话id的最近几条消息)
{
    std::cout<<"测试会话-1中的倒数若干条消息"<<std::endl;
    bite_im::MsgStorageService_Stub stub(channel.get());
    // 进行rpc调用

    bite_im::GetRecentMsgReq req;

    req.set_request_id(bite_im::UUID::uuid());
    req.set_chat_session_id("会话-1");
    req.set_msg_count(4);   // 为什么当数字大于
    // req.set_over_time(boost::posix_time::to_time_t(2024-10-08 08:13:58));

    bite_im::GetRecentMsgRsp resp;
    // 获取一个rpc操作对象
    brpc::Controller cntl;
    // stub调用我们实现的服务类ipml的接口
    stub.GetRecentMsg(&cntl, &req, &resp, nullptr);

    // ASSERT_TRUE(resp.success());

    // 检查一下会话1中是否符合
    auto message_info_list = resp.msg_list();

    for (const auto &message_info : message_info_list)
    {
        std::cout << message_info.message_id() << std::endl;
        std::cout << message_info.chat_session_id() << std::endl;
        std::cout << message_info.timestamp() << std::endl;
        // std::cout << message_info.sender().nickname() << std::endl;  // 我们在测试时的nickname还没有设置这里就是空
        std::cout << message_info.message().message_type() << std::endl;
        switch (message_info.message().message_type())
        {
        case bite_im::MessageType::STRING:
        {
            std::cout << message_info.message().string_message().content() << std::endl;
        }
        break;
        case bite_im::MessageType::IMAGE:
        {
            std::cout << message_info.message().image_message().image_content() << std::endl;
        }
        break;
        case bite_im::MessageType::FILE:
        {
            std::cout << message_info.message().file_message().file_contents() << std::endl;
        }
        break;
        case bite_im::MessageType::SPEECH:
        {
            std::cout << message_info.message().speech_message().file_contents() << std::endl;
        }
        break;
        default:
            LOG_ERROR("文件类型[{}]错误", message_info.message().message_type());
        break;
        }
    }
}

/*TEST(消息子服务测试, 搜索会话的消息)
{

    bite_im::MsgStorageService_Stub stub(channel.get());
    // 进行rpc调用

    bite_im::MsgSearchReq req;

    req.set_request_id(bite_im::UUID::uuid());
    req.set_chat_session_id("会话-1");
    req.set_search_key("蛋");

    bite_im::MsgSearchRsp resp;
    // 获取一个rpc操作对象
    brpc::Controller cntl;
    // stub调用我们实现的服务类ipml的接口
    stub.MsgSearch(&cntl, &req, &resp, nullptr);

    // ASSERT_TRUE(resp.success());

    // 检查一下会话1中是否符合
    auto message_info_list = resp.msg_list();

    for (const auto &message_info : message_info_list)
    {
        std::cout << message_info.message_id() << std::endl;
        std::cout << message_info.chat_session_id() << std::endl;
        std::cout << message_info.timestamp() << std::endl;
        std::cout << message_info.sender().nickname() << std::endl;
        std::cout << message_info.message().string_message().content() << std::endl;
    }
}
*/

int main(int argc, char *argv[])
{
    testing::InitGoogleTest(&argc, argv);
    google::ParseCommandLineFlags(&argc, &argv, true);
    bite_im::initLogger(FLAGS_log_mode, FLAGS_log_file, FLAGS_log_level);
    auto manager = std::make_shared<bite_im::ServiceManager>();
    manager->declareService(FLAGS_service);
    // 构造服务发现对象
    auto put_cb = std::bind(&bite_im::ServiceManager::onServiceOnline, manager.get(),
                            std::placeholders::_1, std::placeholders::_2);
    auto del_cb = std::bind(&bite_im::ServiceManager::onServiceOffLine, manager.get(),
                            std::placeholders::_1, std::placeholders::_2);
    auto discoverer = std::make_shared<bite_im::Discovery>(FLAGS_base_service, FLAGS_etcd_host, put_cb, del_cb);

    // 获取服务信道
    channel = manager->choose(FLAGS_service);
    if (channel.get() == nullptr)
    {
        return -1;
    }
    // 我们直接在测试样例中创建Service服务存根

    return RUN_ALL_TESTS();
}